package com.mattercentric;

import java.util.Date;

import org.slim3.datastore.Datastore;

import com.gae.GoogleUser;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.opensymphony.xwork2.ActionSupport;

public class IndexAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String logoutURL;

	public String getLogoutURL() {
		return logoutURL;
	}

	public void setLogoutURL(String logoutURL) {
		this.logoutURL = logoutURL;
	}

	private GoogleUser googleUser;

	public GoogleUser getGoogleUser() {
		return googleUser;
	}

	public void setGoogleUser(GoogleUser googleUser) {
		this.googleUser = googleUser;
	}
	
	private boolean activityLogged;

	public boolean isActivityLogged() {
		return activityLogged;
	}

	public void setActivityLogged(boolean activityLogged) {
		this.activityLogged = activityLogged;
	}

	@Override
	public String execute() {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		
		googleUser = new GoogleUser();
		
		this.googleUser.setAuthDomain(user.getAuthDomain());
		this.googleUser.setEmail(user.getEmail());
		this.googleUser.setFederatedIdentity(user.getFederatedIdentity());
		this.googleUser.setNickName(user.getNickname());
		this.googleUser.setUserID(user.getUserId());
		this.googleUser.setLoggedIn(new Date());
		
		activityLogged = saveUserActivity(googleUser);

		this.logoutURL = userService.createLogoutURL("");

		return SUCCESS;
	}

	public boolean saveUserActivity(GoogleUser googleUser) {

		Key key = Datastore.put(googleUser);

		if (key.isComplete()) {

			return true;

		} else {

			return false;

		}

	}

}
